<?php

declare(strict_types=1);
/**
 * This file is part of Hyperf.
 *
 * @link     https://www.hyperf.io
 * @document https://hyperf.wiki
 * @contact  group@hyperf.io
 * @license  https://github.com/hyperf/hyperf/blob/master/LICENSE
 */
namespace App\Controller;

use App\Model\Good;
use App\Model\Tuan;
use App\Request\TuanRequest;
use App\Service\TuanService;
use Hyperf\DbConnection\Db;
use Hyperf\Di\Annotation\Inject;
use Hyperf\HttpServer\Annotation\Controller;
use Hyperf\HttpServer\Annotation\GetMapping;
use Hyperf\HttpServer\Annotation\PostMapping;
use App\Middleware\Auth\ValidationLoginMiddleware;
use Hyperf\HttpServer\Annotation\Middleware;

/**
 * @Controller()
 * @Middleware(ValidationLoginMiddleware::class)
 * Class TuanController
 */
class TuanController extends AbstractController
{
    /**
     * @Inject
     * @var TuanRequest
     */
    protected $tuanRequest;

    /**
     * @Inject
     * @var TuanService
     */
    protected $tuanService;

    /**
     * @GetMapping(path="new_tuan")
     * @return array
     */
    public function newTuan()
    {
        $goods_id = $this->request->input('goods_id');
        $goods = Good::find($goods_id);

        if (empty($goods)) {
            return $this->failed('商品不存在');
        }

        $tuan = Tuan::query()->where('goods_id', $goods_id)
            ->where('status', 0)
            ->where('create_time', '>', 1630941300)
            ->limit(3)
            ->get();

        if (count($tuan) == 0) {
            $this->tuanService->create(['goods_id' => $goods_id]);

            $tuan = Tuan::query()->where('goods_id', $goods_id)
                ->where('status', 0)
                ->where('create_time', '>', 1630941300)
                ->limit(3)
                ->get();
        }

        $data = [];

        foreach ($tuan as $k => $v) {
            $t = [
                'yet_number' => $v->tuan_add_num,
                'need_number' => $v->need_num,
                'id' => $v->id,
            ];

            $data[] = $t;
        }

        return $this->success($data, '进行中的团');
    }

    /**
     * @GetMapping(path="get_list_tuan_people_num")
     */
    public function getTuanPeopleNum(): array
    {
        $get = $this->request->all();

        if (! isset($get['goods_id'])) {
            return $this->failed('无商品ID');
        }

        $goods = Good::find($get['goods_id']);

        $tuan_people_num = $goods->tuan_people_num;

        $tuan_str = '还差' . rand(1, 4) . '成团';

        $bt_money = $this->tuanService->tuan_bt_money($goods->tuan_reward, $tuan_people_num);

        return $this->success(['tuan_str' => $tuan_str, 'bt_money' => $bt_money['bt_money'], 'o' => $get['o'] ?? 1, 'o2' => $get['o2'] ?? 1]);
    }

    /**
     * 支付
     * @PostMapping(path="pay")
     */
    public function pay(): array
    {
        $post = $this->request->post();

        try {
            $id = __FUNCTION__ . ($post['tuan_id'] ?? '0');

            $result = $this->try($id, function () use ($post) {

                Db::beginTransaction();
                try {
                    $this->tuanRequest->payCheck($post);
                    $result =  $this->tuanService->pay($post);

                    Db::commit();

                    return $result;
                }catch (\Exception $exception){
                    Db::rollBack();
                    throw new \Exception($exception->getMessage());
                }
            });

            return $this->success($result, '支付信息');

        } catch (\Exception $e) {

            return $this->failed($e->getMessage());
        }
    }
}
